//
//  AnnouncementDetailViewController.m
//  DaDaBao
//
//  Created by User on 15/8/18.
//  Copyright (c) 2015年 User. All rights reserved.
//

#import "AnnouncementDetailViewController.h"
#import "AnnouncementModel.h"

@interface AnnouncementDetailViewController ()<UIWebViewDelegate>

@property (nonatomic, strong) AnnouncementModel *announcementModel;

@property (nonatomic, strong) UIScrollView *backgroundScrollView;

@end

@implementation AnnouncementDetailViewController

- (id)initWithModel:(AnnouncementModel *)annoucementModel
{
    self = [super init];
    if(self)
    {
        _announcementModel = annoucementModel;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self createUI];
    
    }
-(void)createUI
{
    TopView *topView = [[TopView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, SCREEN_WIDTH, 64.0f)];
    [topView setTitile:@"服务公告"];
    [self.view addSubview:topView];
    
    UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(0.0f, 20.0f, 64.0f, 44.0f)];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(buttonBackTapped:) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:backBtn];
    
    _backgroundScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0.0f, 64.0f, SCREEN_WIDTH, SCREEN_HEIGHT - 64.0f)];
    [self.view addSubview:_backgroundScrollView];
    
    CGFloat titleLabHeight = [UILabel height:_announcementModel.title widthOfFatherView:SCREEN_WIDTH - 30.0f textFont:[UIFont boldSystemFontOfSize:16.0f]];
    
    UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(15.0f, 10.0f, SCREEN_WIDTH - 30.0f, titleLabHeight)];
    titleLab.backgroundColor = [UIColor clearColor];
    titleLab.font = [UIFont boldSystemFontOfSize:16.0f];
    titleLab.numberOfLines = 0;
    titleLab.text = _announcementModel.title;
    [_backgroundScrollView addSubview:titleLab];
    
    UILabel *timeLab = [[UILabel alloc]initWithFrame:CGRectMake(15.0f, titleLab.frame.origin.y + titleLab.frame.size.height + 5.0f, SCREEN_WIDTH - 30.0f, 20.0f)];
    timeLab.textColor = [UIColor lightGrayColor];
    timeLab.backgroundColor = [UIColor clearColor];
    timeLab.font = [UIFont systemFontOfSize:15.0f];
    timeLab.text = _announcementModel.publishTime;
    [_backgroundScrollView addSubview:timeLab];
    
    UILabel *sepLab = [[UILabel alloc]initWithFrame:CGRectMake(0.0f, timeLab.frame.origin.y + timeLab.frame.size.height + 13.0f, SCREEN_WIDTH, 0.5f)];
    sepLab.backgroundColor = [UIColor grayColor];
    [_backgroundScrollView addSubview:sepLab];
    
    UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(5.0f, sepLab.frame.origin.y + sepLab.frame.size.height + 5.0f, SCREEN_WIDTH - 10.0f, SCREEN_HEIGHT - sepLab.frame.origin.y - sepLab.frame.size.height - 5.0f)];
    webView.delegate = self;
    webView.scrollView.showsHorizontalScrollIndicator = FALSE;
    webView.scrollView.showsVerticalScrollIndicator = FALSE;
    [webView loadHTMLString:_announcementModel.content baseURL:nil];
    webView.scrollView.scrollEnabled = FALSE;
    [_backgroundScrollView addSubview:webView];

}
#pragma mark  webDelegate

- (void)webViewDidStartLoad:(UIWebView *)webView
{
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    float totalHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollHeight"] floatValue];
    
    webView.frame = CGRectMake(webView.frame.origin.x, webView.frame.origin.y, webView.frame.size.width, totalHeight);

    _backgroundScrollView.contentSize = CGSizeMake(_backgroundScrollView.frame.size.width, webView.frame.origin.y + totalHeight );
}

- (void)buttonBackTapped:(id)sender
{
    [self.navigationController popViewControllerAnimated:TRUE];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
